package com.zhou.uploadServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.exception.UpException;
import com.zhou.exception.UpExceptionEnum;
import com.zhou.uploadServer.dao.FileUploadMapper;
import com.zhou.uploadServer.model.FileUpload;
import com.zhou.uploadServer.resp.FileUploadResp;
import com.zhou.uploadServer.service.FileUploadService;
import com.zhou.utils.CopyUtil;
import com.zhou.utils.GetFileMsg;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: FileUploadServiceImpl
 * @Description:
 * @Author: admin
 * @Date: 2021/12/22 21:36
 */
@Service
public class FileUploadServiceImpl extends ServiceImpl<FileUploadMapper, FileUpload> implements FileUploadService {

    @Resource
    private FileUploadMapper fileUploadMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");


    /**
     * @Description: 文件保存方法
     * @Author: jz
     * @Date: 2021/12/23 10:05
     * @Param:
     * @Return:
     */
    @Override
    @Transactional
    public FileUpload fileUpload(MultipartFile file) {

        GetFileMsg getFileMsg = new GetFileMsg();
        FileUpload fileMsg = null;
        try {
            fileMsg = getFileMsg.getMsg(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new UpException(UpExceptionEnum.FIND_FAIL);
        }

        //获取文件后缀名
        String fileType = fileMsg.getFileType();

        //生成服务器存储文件的文件夹位置
        String savePath = fileMsg.getSaveFolder();


        //对路径进行解码
        try {
            savePath = java.net.URLDecoder.decode(savePath, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new UpException(UpExceptionEnum.SYSTEM_EXCEPTION);
        }
        fileMsg.setSaveFolder(savePath);

        //生成路径
        File folder = new File(savePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        //通过uuid生成文件名
        String name = fileMsg.getNewName();
        String newFileName = name + fileType;

        //进行文件创建
        try {
            file.transferTo(new File(folder, newFileName));
        } catch (IOException e) {
            e.printStackTrace();
            throw new UpException(UpExceptionEnum.UPLOAD_FAILID);
        }


        return fileMsg;

    }


    /**
     * @Description: 文件信息入库
     * @Author: jz
     * @Date: 2021/12/23 10:06
     * @Param:
     * @Return:
     */
    @Transactional
    @Override
    public void ins(FileUpload fileUpload) {
        //进行信息插入
        int insert = fileUploadMapper.insert(fileUpload);
        if (insert == 0) {
            throw new UpException(UpExceptionEnum.INSERT_FAIL);
        }
    }


    /**
     * @Description: 获取文件信息
     * @Author: jz
     * @Date: 2021/12/23 10:06
     * @Param:
     * @Return:
     */
    @Override
    public FileUploadResp getFileData(String fileName) {
        //时间转换格式
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //构建查询条件
        QueryWrapper<FileUpload> fileUploadQueryWrapper = new QueryWrapper<>();
        fileUploadQueryWrapper.eq("new_name", fileName);
        FileUpload fileUpload = fileUploadMapper.selectOne(fileUploadQueryWrapper);

        //进行时间格式的转换
        String format = date.format(fileUpload.getCreateTime());;
        //判断是否查询成功
        if (fileUpload == null) {
            throw new UpException(UpExceptionEnum.QUERY_FAIL);
        }
        //拷贝信息到返回类
        FileUploadResp fileUploadResp = CopyUtil.copy(fileUpload, FileUploadResp.class);
        fileUploadResp.setCreateTime(format);
        return fileUploadResp;
    }
}
